package cn.com.Servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Servlet2",urlPatterns = "/Servlet2",
        initParams = {@WebInitParam(name = "encoding", value ="UTF-8" )})
public class Servlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //下面三行是网上找的PrintWriter输出中文乱码的解决办法
        resp.setHeader("content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        req.setCharacterEncoding("UTF-8");
        PrintWriter out=resp.getWriter();
        //获得ServletConfig对象
        ServletConfig config=this.getServletConfig();
        //获得参数名为encodin对应的参数值
        String param=config.getInitParameter("encoding");

        out.println("encoding="+param);
        out.println("<br>");
        out.println("2107361202 齐小康");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
